package com.superzlc.miniweb.comn;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configuation {
	private static Properties props;

	static {
		// 载入配置
		props = new Properties();
		InputStream inputStream = Thread.currentThread()
				.getContextClassLoader()
				.getResourceAsStream("props.properties");
		try {
			props.load(inputStream);
		} catch (IOException e) {
		}
	}

	public static String get(String key) {
		if (!props.containsKey(key)) {
			return null;
		}
		return props.getProperty(key);
	}

	public static String get(String key, String defval) {
		if (!props.containsKey(key)) {
			return defval;
		}
		return props.getProperty(key, defval);
	}

	public static String getString(String key) {
		if (!props.containsKey(key)) {
			return null;
		}
		return props.getProperty(key);
	}

	public static String getString(String key, String defval) {
		if (!props.containsKey(key)) {
			return defval;
		}
		return props.getProperty(key, defval);
	}

	public static int getInt(String key) {
		if (!props.containsKey(key)) {
			return 0;
		}
		String val = props.getProperty(key);
		try {
			return Integer.parseInt(val);
		} catch(NumberFormatException e) {
			return 0;
		}
	}

	public static int getInt(String key, int defval) {
		if (!props.containsKey(key)) {
			return defval;
		}
		String val = props.getProperty(key);
		try {
			return Integer.parseInt(val);
		} catch(NumberFormatException e) {
			return defval;
		}
	}

	public static boolean getBoolean(String key) {
		if (!props.containsKey(key)) {
			return false;
		}
		String val = props.getProperty(key);
		return Boolean.parseBoolean(val);
	}

	public static boolean getBoolean(String key, boolean defval) {
		if (!props.containsKey(key)) {
			return defval;
		}
		String val = props.getProperty(key);
		return Boolean.parseBoolean(val);
	}

}
